package v2

import (
	"fmt"
	"io/ioutil"
	"os"
)

type DataSource interface {
	Write(string)
	Read() string
}

type FileDataSource struct {
	Path string
}

func (f *FileDataSource) Write(s string) {
	file, err := os.OpenFile(f.Path, os.O_CREATE|os.O_RDWR, os.ModePerm)
	if err != nil {
		panic(err)
	}
	defer file.Close()
	n, err := file.Write([]byte(s))
	if err != nil {
		panic(err)
	}
	fmt.Printf("write length: %d\n", n)
}

func (f *FileDataSource) Read() string {
	file, err := os.Open(f.Path)
	if err != nil {
		panic(err)
	}

	contents, err := ioutil.ReadAll(file)
	if err != nil {
		panic(err)
	}
	return string(contents)
}
